package com.kidback.bbloc259.ui;

import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.kidback.bbloc259.base.BaseActivity;
import com.kidback.bbloc259.databinding.MainActivityBinding;
import com.kidback.bbloc259.ui.home.HomeFragment;
import com.kidback.bbloc259.ui.map.MapFragment;
import com.kidback.bbloc259.util.CollectionUtils;
import com.kidback.bbloc259.util.StatusBarUtils;

public class MainActivity extends BaseActivity<MainActivityBinding> {

    public static int DEFAULT_TAB_INDEX = 0;

    private Fragment[] tabFragmentArr = new Fragment[3];
    private Fragment currentFragment;
    private long lastClickTimeStamp = 0;
    private ViewGroup[] tabViewGroup = null;
    private View[] tabViews = null;


    @Override
    public MainActivityBinding getViewBinding() {
        return MainActivityBinding.inflate(inflater);
    }

    @Override
    protected void initView() {
        StatusBarUtils.setStatusBarColor(activity, 0xff715af3);

        tabViewGroup = new ViewGroup[]{viewBinding.tab1, viewBinding.tab2, viewBinding.tab3};
        tabViews = new View[]{viewBinding.tab1, viewBinding.tab2, viewBinding.tab3};

        for (int i = 0; i < CollectionUtils.size(tabViewGroup); i++) {
            tabViewGroup[i].setOnClickListener(tabClickListener);
        }

        clickTab(DEFAULT_TAB_INDEX);

    }

    @Override
    protected void loadData() {

    }


    private View.OnClickListener tabClickListener = v -> {
        for (int i = 0; i < tabViews.length; i++) {
            if (v == tabViews[i]) {
                clickTab(i);
                break;
            }
        }
    };


    private void clickTab(int tabIndex) {
        if (tabFragmentArr[tabIndex] == null) {
            switch (tabIndex) {
                case 0:
                    tabFragmentArr[tabIndex] = new HomeFragment();
                    break;
                case 1:
                    tabFragmentArr[tabIndex] = new MapFragment();
                    break;
                case 2:
                    tabFragmentArr[tabIndex] = new MineFragment();
                    break;
            }
        }

        Fragment targetFragment = tabFragmentArr[tabIndex];

        if (targetFragment != null && targetFragment != currentFragment) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction transaction = fm.beginTransaction();
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
            String tag = targetFragment.getClass().getSimpleName();
            if (fm.findFragmentByTag(tag) == null) {
                transaction.add(viewBinding.homeContainer.getId(), targetFragment, tag);
            } else {
                transaction.show(targetFragment);
            }

            currentFragment = targetFragment;
            transaction.commit();
        }

        //处理 tab 选中颜色
        for (int i = 0; i < CollectionUtils.size(tabViewGroup); i++) {
            tabViewGroup[i].setSelected(false);

            int childCount = tabViewGroup[i].getChildCount();
            for (int j = 0; j < childCount; j++) {
                tabViewGroup[i].getChildAt(j).setSelected(false);
            }
        }

        tabViewGroup[tabIndex].setSelected(true);
        int childCount = tabViewGroup[tabIndex].getChildCount();
        for (int j = 0; j < childCount; j++) {
            tabViewGroup[tabIndex].getChildAt(j).setSelected(true);
        }


    }


}
